package com.wf.smartcomm.server.admin.action;

import java.util.Date;
import java.util.HashMap;

import javax.annotation.Resource;

import org.json.JSONObject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
import com.wf.smartcomm.server.bean.MerchantUser;
import com.wf.smartcomm.server.service.MerchantUserService;
import com.wf.smartcomm.server.util.IdUtils;

@Controller
@Scope("prototype")
public class MerchantUserAction extends BaseAction implements ModelDriven<MerchantUser> {
	
	private MerchantUser merchantUser = new MerchantUser();
	
	@Resource
	private MerchantUserService merchantUserService;
	
	public void save()throws Exception{
		merchantUser.setObjectId(IdUtils.uuid());
		merchantUser.setCreatedAt(new Date());
		merchantUser.setUpdatedAt(merchantUser.getCreatedAt());
		merchantUser.setCreatedBy(currentUser);
		merchantUser.setUpdatedBy(currentUser);
		merchantUserService.save(merchantUser);
	}
	
	public void list() throws Exception {
		String aoData = request.getParameter("aoData");
		String sEcho = "";
		String iDisplayStart = "";
		String iDisplayLength = "";
		JSONObject jo = new JSONObject(aoData);
		sEcho = jo.getString("sEcho");
		iDisplayStart = jo.getString("iDisplayStart");
		iDisplayLength = jo.getString("iDisplayLength");
		//sSearch = jo.getString("sSearch");
		page.setBeginIndex(Integer.parseInt(iDisplayStart));
		page.setPageSize(Integer.parseInt(iDisplayLength));
		int  initEcho = Integer.parseInt(sEcho)+1;
		merchantUserService.list(page);
		int count = page.getTotalCount();
		HashMap<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("iTotalRecords", count);
		dataMap.put("sEcho",initEcho);
		dataMap.put("iTotalDisplayRecords", count);
		dataMap.put("aaData", page.getDataList());
		printJsonData(outGson.toJson(dataMap));
	}

	@Override
	public MerchantUser getModel() {
		return merchantUser;
	}
}
